var datagrid=require('datagrid');
var template=require('template');
var layer=require('layer');
var request=require('request');
var ztree=require('{public}script/jquery.ztree.all');
var taxcode=require('../views/public/taxcode');

return function(param){
    var that=this;
    var _layer=layer({
        content:template.render(taxcode,{}),
        title:'税收编码选择',
        isMove:false,
        isClickMask:true,
        id:'taxclassify',
        width:1000,
        height:644,
        cancel:{
            enable:false
        },
        events:{
            //点击关键字列表
            'click .m-keyword li a':'keyList query',
            'input .m-searchName':'isInput',
            'click .m-searchIcon':'search keyword query',
            'keyup .m-searchName':'search keyword query',
            'dblclick .table-tbody tr':'layerhide'
        },
        onInit:function(self){
            //生成树状图
            this.treeNode();
            this.keyword(); //关键词列表
            this.grid(); //初始化数据网格
            if(param){
                $('.m-searchName').val(param.name);
            }
        },
        grid:function(){
            this.gridList=datagrid({
                container:'.m-goodList',
                fields:true,
                placeholder:'没有商品信息！',
                dataField:'res',
                isBorder:true,
                paging:{
                    vars:{
                        aCount:'totalCount'
                    },
                    url:'/product/tax_kind/search.action',
                    ajax:{
                        type:'post'
                    },
                    condition:param
                },
                columns:[{
                    showtitle:true,
                    title:'编码',
                    content:'',
                    width:100,
                    field:'BM'
                }, {
                    showtitle:true,
                    title:'合并编码',
                    content:'',
                    width:180,
                    field:'HBBM'
                }, {
                    showtitle:true,
                    title:'名称',
                    field:'MC',
                    width:150
                }, {
                    showtitle:true,
                    title:'说明',
                    width:150,
                    field:'SM',
                    filter:function(val,field,data){
                        return val==undefined ? '--' : val;
                    }
                }, {
                    showtitle:true,
                    title:'税率',
                    width:50,
                    field:'SLV',
                    filter:function(val,field,data){
                        return val==undefined ? '--' : val;
                    }
                }, {
                    showtitle:true,
                    title:'用户选择比例',
                    width:150,
                    field:'precent'
                }, {
                    showtitle:true,
                    title:'建议',
                    width:80,
                    field:'marks',
                    filter:function(val,filed,data){
                        return val==undefined ? '--' : val;
                    }
                }, {
                    showtitle:true,
                    title:'增值税特别管理',
                    width:120,
                    field:'ZZSTSGL',
                    filter:function(val,filed,data){
                        return val==undefined ? '--' : val;
                    }
                }, {
                    showtitle:true,
                    title:'增值税政策依据',
                    width:120,
                    field:'DJ',
                    filter:function(val,filed,data){
                        return val==undefined ? '--' : val;
                    }
                }, {
                    showtitle:true,
                    title:'增值税特殊内容代码',
                    width:130,
                    field:'HSJBZ',
                    filter:function(val,field,data){
                        return ['否','是'][val];
                    }
                }, {
                    showtitle:true,
                    title:'关键字',
                    width:100,
                    field:'GJZ',
                    filter:function(val,field,data){
                        return val==undefined ? '--' : val;
                    }
                }, {
                    showtitle:true,
                    title:'统计局编码',
                    width:100,
                    field:'YHZC',
                    filter:function(val,field,data){
                        return val==undefined ? '--' : val;
                    }
                },{
                    showtitle:true,
                    title:'享受优惠政策',
                    width:100,
                    field:'YHZC',
                    filter:function(val,field,data){
                        return ['否','是'][val];
                    }
                }, {
                    showtitle:true,
                    title:'优惠政策类型',
                    width:100,
                    field:'YHZCMC'
                },{
                    showtitle:true,
                    title:'享受优惠政策',
                    width:100,
                    field:'YHZC',
                    filter:function(val,field,data){
                        return ['否','是'][val];
                    }
                },{
                    showtitle:true,
                    title:'使用汇率',
                    width:100,
                    field:'YHZC',
                    filter:function(val,field,data){
                        return ['否','是'][val];
                    }
                },{
                    showtitle:true,
                    title:'汇总项',
                    width:100,
                    field:'YHZC',
                    filter:function(val,field,data){
                        return ['否','是'][val];
                    }
                },{
                    showtitle:true,
                    title:'是否隐藏',
                    width:100,
                    field:'ISHIDE',
                    filter:function(val,field,data){
                        return ['否','是'][val];
                    }
                },{
                    showtitle:true,
                    title:'消费税管理',
                    width:100,
                    field:'YHZC',
                    filter:function(val,field,data){
                        return val==undefined ? '--' : val;
                    }
                },{
                    showtitle:true,
                    title:'消费税政策依据',
                    width:130,
                    field:'YHZC',
                    filter:function(val,field,data){
                        return val==undefined ? '--' : val;
                    }
                },{
                    showtitle:true,
                    title:'消费税特殊内容代码',
                    width:150,
                    field:'YHZC',
                    filter:function(val,field,data){
                        return val==undefined ? '--' : val;
                    }
                }],
                onRowRender:function(self, val, i){
                    //self.addClass('s-err')
                    console.log(i)
                    $('.table-tbody tr').eq(i).addClass('allowClick');
                    //self._options.className='allowClick';
                }   
            });
            this.paging = this.gridList.paging;
        },
        // 关键字列表
        keyword:function(e,elem,data){
            data=param ? extend(param,data) :data;
            request.post('/product/tax_kind/keys.action',data,function(res){
                var li='';
                Nui.each(res,function(val,key){
                    li+='<li><a href="javascript:;" title="'+val.u_name+'">'+val.u_name+'</a></li>';
                });
                _layer.main.find('.m-keyword').html(li);
            });
            return data;
        },
         //生成树状图
        treeNode:function(e,elem,data){
            var that=this;
            var setting = {
                view: {
                    selectedMulti: false,	//不可同时选择多个节点
                    showIcon:false
                },
                async: {	//设置异步加载
                    enable: true,
                    url:serviceUrl+'/product/tax_kind/navigation/list.action',	//传参地址，可添加参数
                    dataType:"json",
                    type:"post",
                    autoParam:['id','name']	//提交父节点的参数
                },
                callback: {
                    onClick:function(event, treeId, treeNode){
                        console.log(treeNode)
                        console.log(this)
                        $('.m-searchName').val('');
                        var searchParam={
                            name:treeNode.name,
                            id:treeNode.id
                        }
                        that.paging.condition=searchParam;
                        that.paging.query(true);
                    }
                }
            };
            $.fn.zTree.init($('#treeDemo'), setting);
        },
        keyList:function(e,elem,data){
            var searchName=$.trim(elem.attr('title'));
            $('.m-searchName').val(searchName);
            return {name:searchName};
        },
        // 判断input是否有值，search按钮是否显示
        isInput:function(e,elem,data){
            var searchName=$.trim(elem.val());
            if(searchName.length){
                $('.m-searchIcon').removeClass('f-dn');
            }else{
                $('.m-searchIcon').addClass('f-dn');
            }
        },
        search:function(e,elem,data){
            var searchName=$.trim(elem.closest('.m-search').find('.m-searchName').val());
            return (searchName!='' && (e.type=='click' || e.keyCode=='13')) ? {name:searchName} : false;
        },
        layerhide:function(e,elem,data){
            //汇总项字段
            if(elem.data('HZX')=='N'){

            }else{
                _layer.hide();
            }
        },
        query:function(e,elem,data){
            this.paging.condition=data;
            this.paging.query(true);
        }
    });
    
}